package com.me.supermario.entities;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;

public class FireFlower extends Sprite {

	//VARIE ED EVENTUALI
	private Vector2 velocity = new Vector2 ();
	private TiledMapTileLayer collisionLayer;
	protected Rectangle rect = new Rectangle ();

	//VARIABILI FLOAT
	private float speed = 60;
	private float gravity = 200 * 1.8f;
	private float increment;

	//STRINGHE E PROPRIETA' DEI TILE
	private String blockedKey = "blocked";

	//VARIABILI BOOLEANE
	private boolean deadState;
	
	//SPRITE
	private static Sprite sprite = new Sprite(new Texture("com/me/img/fireFlowersheet.png"));

	//COSTRUTTORE
	public FireFlower (TiledMapTileLayer collisionLayer) {
		super(sprite);
		this.collisionLayer = collisionLayer;
	}//costruttore

	//DRAW
	public void draw (SpriteBatch spritebatch) {
		update(Gdx.graphics.getDeltaTime());
		super.draw(spritebatch);
	}//draw

	private void update(float delta) {
		//System.out.println ("entro in update");
		velocity.y -= gravity*delta;

		//salva le vecchie posizioni
		float oldY = getY();
		//settiamo due valori booleani: uno per vedere se collidiamo nell'asse X, uno per l'asse Y
		boolean collisionY = false;

		// calculate the increment for step in #collidesLeft() and #collidesRight()
		increment = collisionLayer.getTileWidth();
		increment = getWidth() < increment ? getWidth() / 2 : increment / 2;

		// muovi in Y
		setY(getY() + velocity.y * delta * 5f);

		// calculate the increment for step in #collidesBottom() and #collidesTop()
		increment = collisionLayer.getTileHeight();
		increment = getHeight() < increment ? getHeight() / 2 : increment / 2;

		if(velocity.y < 0){ // stai andando giu'
			collisionY = collidesBottom();

		}
		else if(velocity.y > 0) // stai andando su
			collisionY = collidesTop();

		// gestisci la collisione in Y
		if(collisionY) {
			setY(oldY);
			velocity.y = 0;
		}
	}//update

	protected Rectangle rectangle() {
		return new Rectangle(this.getX(), this.getY(), this.getWidth(), this.getHeight());
	}//rectangle

	public void deadByTrample() {
		velocity.set(0, 0);
		deadState = true;
	}//deadByTrample


	//METODI DI COLLISIONE
	private boolean isCellBlocked(float x, float y) {
		Cell cell = collisionLayer.getCell((int) (x / collisionLayer.getTileWidth()), (int) (y / collisionLayer.getTileHeight()));
		return cell != null && cell.getTile() != null && cell.getTile().getProperties().containsKey(blockedKey);
	}//isCellBlocked

	public boolean collidesRight() {
		for(float step = 0; step < getHeight(); step += increment)
			if(isCellBlocked(getX() + getWidth(), getY() + step))
				return true;
		return false;
	}//collidesRight

	public boolean collidesLeft() {
		for(float step = 0; step < getHeight(); step += increment)
			if(isCellBlocked(getX(), getY() + step))
				return true;
		return false;
	}//collidesLeft

	public boolean collidesTop() {
		for(float step = 0; step <getWidth(); step += increment)
			if(isCellBlocked(getX() + step, getY() + getHeight())){
				return true;
			}
		return false;
	}//collidesTop

	public boolean collidesBottom() {
		for(float step = 0; step < getWidth(); step += increment)
			if(isCellBlocked(getX() + step , getY() ))
				return true;
		return false;
	}//collidesBottom

	public TiledMapTileLayer getCollisionLayer() {
		return collisionLayer;
	}//getCollisionLayer
	
	public boolean getDeadState() {
		return deadState;
	}//getDeadState



}
